document.write('<script type="text/javascript" src="../../libs/validation/jquery.validate.min.js"></script>');
document.write('<script type="text/javascript" src="../../libs/validation/additional-methods.min.js"></script>');
document.write('<script type="text/javascript" src="../../libs/validation/localization/messages_zh.min.js"></script>');

$(document).ready(function(){
    jQuery.validator.addMethod("trim", function(value, element, param) {
        if(param){
            value=$.trim(value);
        }
        return this.optional(element) || (value.length!=0);
    }, $.validator.format("该字段不能为空"));

    //添加用户校验规则
    var validatorForm = $("#form").validate( {
        errorElement: 'span',
        errorClass: 'text-error',
        rules: {
            userName: {
                required: true,
                rangelength: [2,20],
                trim:true
            },
            loginCode: {
                required: true,
                rangelength: [1,16],
                trim:true
            },
            mobile: {
                required: true,
                isMobile:true,
                trim:true
            },
            organizationId: {
                required: true,
                trim:true
            },
            email: {
                required: true,
                email:true
            },
            roleId: {
                required: true,
                trim:true
            },
            orgName:{
                required: true,
                trim:true
            },
            roleName:{
                required: true,
                trim:true
            },
            paramTypeName:{
                required: true,
                trim:true
            },
            paramTypeValue:{
                required: true,
                trim:true
            },
            paramName:{
                required: true,
                trim:true
            },
            paramValue:{
                required: true,
                trim:true
            },
            groupCode:{
                required: true,
                trim:true
            },
            functionName:{
                required: true,
                trim:true
            },
            functionCode:{
                required: true,
                trim:true
            },
            menuName:{
                required: true,
                trim:true
            },
            sortNo:{
                required: true,
                trim:true
            },
            viewPermission:{
                required: true,
                trim:true
            },
            menuUrl:{
                required: true,
                trim:true
            }
        },
        messages: {
            userName: {
                required: '用户名不能为空',
                trim:'用户名不能为空'
            },
            loginCode: {
                required: '帐号不能为空',
                trim:'帐号不能为空'
            },
            mobile: {
                required: '手机号码不能为空',
                isMobile:'请输入正确的手机号码',
                trim:'手机号码不能为空'
            },
            organizationId: {
                required: '所属部门不能为空',
                trim:'所属部门不能为空'
            },
            email: {
                required: '电子邮箱不能为空'
            },
            roleId: {
                required: '所属角色不能为空',
                trim:'所属角色不能为空'
            },
            orgName:{
                required: '部门名称不能为空',
                trim:'部门名称不能为空'
            },
            roleName:{
                required: '角色名称不能为空',
                trim: '角色名称不能为空'
            },
            paramTypeName:{
                required: '参数类型名称不能为空',
                trim:'参数类型名称不能为空'
            },
            paramTypeValue:{
                required: '参数类型编码不能为空',
                trim:'参数类型编码不能为空'
            },
            paramName:{
                required: '参数名称不能为空',
                trim:'参数名称不能为空'
            },
            paramValue:{
                required: '参数值不能为空',
                trim:'参数值不能为空'
            },
            groupCode:{
                required: '分组名称不能为空',
                trim:'分组名称不能为空'
            },
            functionName:{
                required: '权限名称不能为空',
                trim:'权限名称不能为空'
            },
            functionCode:{
                required: '权限编码不能为空',
                trim:'权限编码不能为空'
            },
            menuName:{
                required: '菜单名称不能为空',
                trim:'菜单名称不能为空'
            },
            sortNo:{
                required: '排序号不能为空',
                trim:'排序号不能为空'
            },
            viewPermission:{
                required: '浏览权限不能为空',
                trim:'浏览权限不能为空'
            },
            menuUrl:{
                required: '链接地址不能为空',
                trim:'链接地址不能为空'
            }
        },
        errorPlacement: function ( error, element ) {
            error.addClass( "ui red pointing label transition" );
            error.insertAfter( element.parent() );
        },
        highlight: function ( element, errorClass, validClass ) {
            $( element ).parents( ".row" ).addClass( errorClass );
            $( element ).focus();
        },
        unhighlight: function (element, errorClass, validClass) {
            $( element ).parents( ".row" ).removeClass( errorClass );
        }
    });



    //修改密码校验规则
    var validatorForm2 = $("#form2").validate( {
        errorElement: 'span',
        errorClass: 'text-error',
        rules: {
            oldPassword: {
                required: true,
                trim:true
            },
            newPassword: {
                required: true,
                trim:true
            },
            confirmPassword: {
                required: true,
                trim:true
            },
            mobile: {
                required: true,
                isMobile:true,
                trim:true
            },
            email: {
                required: true,
                email:true,
                trim:true
            }
        },
        messages:{
            oldPassword: {
                required: '原密码不能为空',
                trim:'原密码不能为空'
            },
            newPassword: {
                required: '新密码不能为空',
                trim:'新密码不能为空'
            }
        },
        errorPlacement: function ( error, element ) {
            error.addClass( "ui red pointing label transition" );
            error.insertAfter( element.parent() );
        },
        highlight: function ( element, errorClass, validClass ) {
            $( element ).parents( ".row" ).addClass( errorClass );
            $( element ).focus();
        },
        unhighlight: function (element, errorClass, validClass) {
            $( element ).parents( ".row" ).removeClass( errorClass );
        }
    });


    //添加项目校验规则
    var validatorSubform = $("#subform").validate( {
        errorElement: 'span',
        errorClass: 'text-error',
        rules: {
            projectCode: {
                required: true,
                rangelength: [1,50],
                trim:true
            },
            projectName: {
                required: true,
                rangelength: [1,50],
                trim:true
            },
            projectManager: {
                required: true,
                trim:true
            },
            saler: {
                required: true,
                trim:true
            },
            serviceCycle: {
                required: false,
                number: true,
                trim:true
            },
            maintenancePeriod: {
                required: false,
                number: true
            },
            totalProductAmount: {
                required: false,
                number: true,
                range: [0.00,99999999.99],
                min: 0.00,
                minNumber: $("#totalProductAmount").val()
            },
            totalServiceAmount: {
                required: false,
                number: true,
                range: [0.00,99999999.99],
                min: 0.00,
                minNumber: $("#totalProductAmount").val()
            },
            totalContractAmount: {
                required: false,
                number: true,
                range: [0.00,99999999.99],
                min: 0.00,
                minNumber: $("#totalProductAmount").val()
            },
            totalNonTaxAmount: {
                required: false,
                number: true,
                range: [0.00,99999999.99],
                min: 0.00,
                minNumber: $("#totalProductAmount").val()
            },
            productRate:{
                number: true,
                range: [0.00,99.99],
                min: 0.00
            },
            serviceRate:{
                number: true,
                range: [0.00,99.99],
                min: 0.00
            },
            totalProductNonTax:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            totalServiceNonTax:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            productProAmount:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            productProNonTax:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            serviceProAmount:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            serviceProNonTax:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            totalProAmount:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            totalProNonTax:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            implementationSalary:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            salesSalary:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            grossProfit:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            overheadPercent:{
                number: true,
                range: [0.00,99.99],
                min: 0.00
            },
            overhead:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            salesCommission:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            salesCommissionPercent:{
                number: true,
                range: [0.00,99.99],
                min: 0.00
            },
            interestExpenses:{
                number: true,
                range: [0.00,99999999.99],
                min: 0.00
            }
        },
        messages: {
            projectCode: {
                required: '项目编号不能为空',
                trim:'项目编号不能为空'
            },
            projectName: {
                required: '项目名称不能为空',
                trim:'项目名称不能为空'
            },
            projectManager: {
                required: '项目经理不能为空',
                trim:'项目经理不能为空'
            },
            saler: {
                required: '销售负责人不能为空',
                trim:'销售负责人不能为空'
            }
        },
        errorPlacement: function ( error, element ) {
            error.addClass( "ui red pointing label transition" );
            error.insertAfter( element);
        },
        highlight: function ( element, errorClass, validClass ) {
            $( element ).parents( ".row" ).addClass( errorClass );
            //$( element ).focus();
        },
        unhighlight: function (element, errorClass, validClass) {
            $( element ).parents( ".row" ).removeClass( errorClass );
        }
    });


    //自定义号码校验方法
    jQuery.validator.addMethod("isMobile", function(value, element) {
        var length = value.length;
        var mobile = /^1[0-9]{10}$/;
        return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请填写正确的手机号码");

    //自定义validate验证输入的数字小数点位数不能大于两位
    jQuery.validator.addMethod("minNumber",function(value, element){
        var returnVal = true;
        inputZ=value;
        var ArrMen= inputZ.split(".");    //截取字符串
        if(ArrMen.length==2){
            if(ArrMen[1].length>2){    //判断小数点后面的字符串长度
                returnVal = false;
                return false;
            }
        }
        return returnVal;
    },"最小金额为0.01，小数点后最多为两位");         //验证错误信息

    //
    $('.modal').on('hidden.bs.modal', function () {
        if(validatorForm){
            validatorForm.resetForm();
        }
        if(validatorForm2){
            validatorForm2.resetForm();
        }
        if(validatorSubform){
            validatorSubform.resetForm();
        }
    });

    //项目基础信息收款计划验证规则
    $("#costEstimateForm").validate( {
        errorElement: 'span',
        errorClass: 'text-error',
        rules: {
            billingDate: {
                required: true,
                trim:true
            },
            planPaymentDate: {
                required: true,
                trim:true
            },
            paymentType: {
                required: true,
                trim:true
            },
            billingAmount: {
                required: true,
                trim:true,
                range: [0.00,99999999.99],
                min: 0.00
            },
            allocationType: {
                required: true,
                trim:true
            },
            allocationRatio: {
                required: true,
                trim:true,
                range: [0.00,99.99],
                min: 0.00
            }
        },
        messages: {
            billingDate: {
                required: '预计开票日期不能为空',
                trim:'预计开票日期不能为空'
            },
            planPaymentDate: {
                required: '预计收款日期不能为空',
                trim:'预计开票日期不能为空'
            },
            paymentType: {
                required: '预计收款类型不能为空',
                trim:'预计收款类型不能为空'
            },
            billingAmount: {
                required: '预计开票金额不能为空',
                trim:'预计开票金额不能为空'
            },
            allocationType: {
                required: '分摊类型不能为空',
                trim:'分摊类型不能为空'
            },
            allocationRatio: {
                required: '分摊比例不能为空',
                trim:'分摊比例不能为空'
            }
        },
        errorPlacement: function ( error, element ) {
            error.addClass( "ui red pointing label transition" );
            //error.insertAfter( element);
            $(element.parent()).append(error);
        },
        highlight: function ( element, errorClass, validClass ) {
            $( element ).parents( ".row" ).addClass( errorClass );
        },
        unhighlight: function (element, errorClass, validClass) {
            $( element ).parents( ".row" ).removeClass( errorClass );
        }
    });
});
